using System;
using System.Collections.Generic;
using System.Linq;

using Csla;

using EntModApp.EntServices.Business;
using EntModApp.EntServices.IoC;

using DemoApp.DataAccess;

namespace DemoApp.BusinessLayer 
{
  [Serializable]
  public partial class CslaSecurableActionEdit : 
    BusinessBase<CslaSecurableActionEdit>,
    ISecurableAction
  {
    #region [ Business Properties ]

    public static PropertyInfo<Guid> SecurableActionIdPropertyInfo = RegisterProperty(new PropertyInfo<Guid>("SecurableActionId"));
    public Guid SecurableActionId 
    { 
      get
      {
        return GetProperty(SecurableActionIdPropertyInfo);
      }
      private set
      {
        LoadProperty(SecurableActionIdPropertyInfo, value);
      }
    }

    public static PropertyInfo<string> NamePropertyInfo = RegisterProperty(new PropertyInfo<string>("Name"));
    public string Name 
    { 
      get
      {
        return GetProperty(NamePropertyInfo);
      }
      set
      {
        LoadProperty(NamePropertyInfo, value);
      }
    }

    public static PropertyInfo<CslaRoleEditCollection> RolesPropertyInfo = RegisterProperty(new PropertyInfo<CslaRoleEditCollection>("Roles"));
    public CslaRoleEditCollection Roles 
    { 
      get
      {
        return GetProperty(RolesPropertyInfo);
      }
      private set
      {
        LoadProperty(RolesPropertyInfo, value);
      }
    }

    #endregion [ Business Properties ]

    #region [ Validation Rules ]
    #endregion [ Validation Rules ]

    #region [ Authorization Rules ]
    #endregion [ Authorization Rules ]
	
    #region [ Factory Methods ]

    #region [ Root Factory Methods ]
    public static CslaSecurableActionEdit New()
    {
      return DataPortal.Create<CslaSecurableActionEdit>();
    }

    public static void Delete(SecurableActionKey key)
    {
      DataPortal.Delete<SecurableAction>(new SingleCriteria<SecurableAction, SecurableActionKey>(key));
    }

    public static void Delete(Guid SecurableActionId)
    {
      Delete(new SecurableActionKey { SecurableActionId = SecurableActionId });
    }
    #endregion [ Root Factory Methods ]

    #region [ Child Factory Methods ]
    internal static CslaSecurableActionEdit NewChild()
    {
      return DataPortal.CreateChild<CslaSecurableActionEdit>();
    }

    internal static CslaSecurableActionEdit GetSecurableActionEdit(SecurableAction data, SecurableActionLoadSettings loadSettings)
    {
      if (data == null) throw new ArgumentNullException("data");
      if (loadSettings == null) throw new ArgumentNullException("loadSettings");
      CslaSecurableActionEdit result = new CslaSecurableActionEdit();
      result.FetchSecurableAction(data, loadSettings);
      result.MarkAsChild();
      return result;
    }
    #endregion [ Child Factory Methods ]

    private static int ForceInitField = 0;
    protected CslaSecurableActionEdit() 
    {
      ForceInitField = 1;
    }
    #endregion [ Factory Methods ]

    #region [ Data Access ]

    #region [ Root Data Access ]
    protected override void DataPortal_Create()
    {
      base.DataPortal_Create();
    }

    [Transactional(TransactionalTypes.TransactionScope)]
    protected override void DataPortal_Insert()
    {
    }
    
    [Transactional(TransactionalTypes.TransactionScope)]
    protected override void DataPortal_Update()
    {
    }
    
    [Transactional(TransactionalTypes.TransactionScope)]
    protected override void DataPortal_DeleteSelf()
    {
      DataPortal_Delete(new SingleCriteria<SecurableAction, SecurableActionKey>(new SecurableActionKey { SecurableActionId = this.SecurableActionId }));
    }
    
    [Transactional(TransactionalTypes.TransactionScope)]
    private void DataPortal_Delete(SingleCriteria<SecurableAction, SecurableActionKey> criteria)
    {
    }
    #endregion [ Root Data Access ]

    #region [ Child Data Access ]
    protected override void Child_Create()
    {
      base.Child_Create();
    }
    
    private void Child_Fetch(object childData)
    {
    }
    
    private void Child_Insert(object parent)
    {
    }
    
    private void Child_Update(object parent)
    {
    }
    
    private void Child_DeleteSelf(object parent)
    {
    }
    #endregion [ Child Data Access ]

    protected void FetchSecurableAction(SecurableAction data, SecurableActionLoadSettings loadSettings)
    {
      FetchSecurableActionData(data, loadSettings);
      FetchSecurableActionChildren(data, loadSettings);
    }

    protected void FetchSecurableActionData(SecurableAction data, SecurableActionLoadSettings loadSettings)
    {
      this.SecurableActionId = data.SecurableActionId;
      this.Name = data.Name;
    }

    protected void FetchSecurableActionChildren(SecurableAction data, SecurableActionLoadSettings loadSettings)
    {
      if (loadSettings.Inclusion.IncludeRoles)
        this.Roles = CslaRoleEditCollection.GetRoleEditCollection(data.Roles, loadSettings.Inclusion.IncludedRolesSettings);
    }
    #endregion [ Data Access ]

    #region [ Explicit ISecurableAction implementation ]

    Guid ISecurableAction.SecurableActionId
    {
      get { return this.SecurableActionId; }
    }

    string ISecurableAction.Name
    {
      get { return this.Name; }
      set
      {
        this.Name = value;
      }
    }

    IBusinessObjectCollection<IRole> ISecurableAction.Roles
    { 
      get
      {
        return this.Roles;
      }
    }


    bool IBusinessObject.IsReadonly
    {
      get { return false; }
    }

    bool IBusinessObject.IsDirty
    {
      get { return this.IsDirty; }
    }

    #endregion [ Explicit ISecurableAction implementation ]
  }
}
